﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using SQLExecute;


public partial class list_nghiphep : System.Web.UI.Page
{
    User u = new User();
    DataSet ds;
    NghiPhep n = new NghiPhep();
    int id;
    public string strMessage;
    string strFrom = "1/1/1900", strTo = "1/1/1900";
    Common c = new Common();

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack&&!IsCallback)
            if (Session["email"] != null && Session["fullname"] != null 
                && Session["groupid"].ToString()=="3")
            {

                c.InitCombo(cbPhongBan, Convert.ToInt16(Session["groupid"].ToString()), Convert.ToInt16(Session["phongbanid"].ToString()));
                n.List_Nghi_Phep_Theo_PhongBan(cbPhongBan, gdNghiPhep, null, null);
                lUsername.Text = Session["username"].ToString();
            }
           
           else { Response.Redirect("../login.aspx"); }
        else n.List_Nghi_Phep_Theo_PhongBan(cbPhongBan, gdNghiPhep, null, null);


        
    }    
    protected void lnkLogout_Click(object sender, EventArgs e)
    {
        Session.Clear();
        Response.Redirect("../login.aspx");
    }
    protected void gdNghiPhep_CustomButtonCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomButtonCallbackEventArgs e)
    {
        int id = (int)gdNghiPhep.GetRowValues(e.VisibleIndex, "id");
        string sql = string.Empty;
       // string strTruongPhong = (string)gdNghiPhep.GetRowValues(e.VisibleIndex, "dv");
        //DateTime dt = (DateTime)gdNghiPhep.GetRowValues(e.VisibleIndex, "nghiphepdenngay"); ;

        if (e.ButtonID == "btnDelete" )
        {
            sql = "update tbl_nghiphep set active=0 where id='" + id + "'";
            SqlHelper.ExecuteNonQuery(Common.strConnect, CommandType.Text, sql);
            if (txtFromDate.Text != "" && txtToDate.Text != "")
            {
                strFrom = Common.Convert_DDMMYYYY_To_MMDDYYY(txtFromDate.Text);
                strTo = Common.Convert_DDMMYYYY_To_MMDDYYY(txtToDate.Text);
            }



            n.List_Nghi_Phep_Theo_PhongBan(cbPhongBan, gdNghiPhep,DateTime.Parse(strFrom),DateTime.Parse(strTo));
        }
        if (e.ButtonID == "btnEdit")        
            Response.RedirectLocation = "edit_form_phep.aspx?id=" + id;
        
    }
    protected void cbPhongBan_SelectedIndexChanged(object sender, EventArgs e)
    { 
        if (txtFromDate.Text != "" && txtToDate.Text != ""&&cbPhongBan.SelectedIndex>0)
            {
                strFrom = Common.Convert_DDMMYYYY_To_MMDDYYY(txtFromDate.Text);
                strTo = Common.Convert_DDMMYYYY_To_MMDDYYY(txtToDate.Text);
                n.List_Nghi_Phep_Theo_PhongBan(cbPhongBan, gdNghiPhep, DateTime.Parse(strFrom), DateTime.Parse(strTo));
            }
        
         else
              n.List_Nghi_Phep_Theo_PhongBan(cbPhongBan, gdNghiPhep,null,null);
    }
    protected void gdNghiPhep_RowCommand1(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
    {
        int nValue;
        string sql = string.Empty;
        DateTime dt;
        string s1,s2;
        if (e.CommandArgs.CommandName == "DuyetNghiPhep")
        {
            id = (int)gdNghiPhep.GetRowValues(e.VisibleIndex, "nghiphepid");
            string strValue=(string)gdNghiPhep.GetRowValues(e.VisibleIndex,"ns");
            dt = (DateTime)gdNghiPhep.GetRowValues(e.VisibleIndex, "nghipheptungay");

            if (dt > DateTime.Now)
            {
                if (strValue == "Chờ")
                    nValue = 1;
                else nValue = 0;
                n.Duyet_Nghi_Phep(id, nValue,1);
                if (txtFromDate.Text == "" && txtToDate.Text == "")
                    n.List_Nghi_Phep_Theo_PhongBan(cbPhongBan, gdNghiPhep, null, null);
                else
                {
                    s1 = Common.Convert_DDMMYYYY_To_MMDDYYY(txtFromDate.Text);
                    s2 = Common.Convert_DDMMYYYY_To_MMDDYYY(txtToDate.Text);
                    n.List_Nghi_Phep_Theo_PhongBan(cbPhongBan, gdNghiPhep, DateTime.Parse(s1), DateTime.Parse(s2));
                }
            }
        }
        if (e.CommandArgs.CommandName == "XoaNghiPhep")
        {
            id = (int)gdNghiPhep.GetRowValues(e.VisibleIndex, "nghiphepid");
            dt = (DateTime)gdNghiPhep.GetRowValues(e.VisibleIndex, "nghipheptungay");


            if (dt > DateTime.Now)
            {
               
                n.Xoa_Nghi_Phep(id);
                if (txtFromDate.Text == "" && txtToDate.Text == "")
                    n.List_Nghi_Phep_Theo_PhongBan(cbPhongBan, gdNghiPhep, null, null);
                else
                {
                    s1 = Common.Convert_DDMMYYYY_To_MMDDYYY(txtFromDate.Text);
                    s2 = Common.Convert_DDMMYYYY_To_MMDDYYY(txtToDate.Text);
                    n.List_Nghi_Phep_Theo_PhongBan(cbPhongBan, gdNghiPhep, DateTime.Parse(s1), DateTime.Parse(s2));
                }
            }
        }

    }
    protected void btnView_Click(object sender, EventArgs e)
    {
        if (txtFromDate.Text!="" && txtToDate.Text != "")
        {
           n.List_Nghi_Phep_Theo_PhongBan(cbPhongBan, gdNghiPhep
                                         , DateTime.Parse(Common.Convert_DDMMYYYY_To_MMDDYYY(txtFromDate.Text))
                                         , DateTime.Parse(Common.Convert_DDMMYYYY_To_MMDDYYY(txtToDate.Text))
                                         );
        }
    }

}
